home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / mc220.zip / FIBO.C < prev    next >
C/C++ Source or Header  |  1992-02-24  |  546b  |  33 lines

  1. /*
  2.  * Program to calculate a fibonacci number.
  3.  *
  4.  * This demonstrates a heavily RECURSIVE function.
  5.  */
  6. #include \mc\stdio.h
  7.  
  8. #define MAXFIB    24    /* Largest we can do in 16 bits */
  9.  
  10. /*
  11.  * Recursive function to calculate a fibonacci number
  12.  */
  13. unsigned fibo(num)
  14.     unsigned num;
  15. {
  16.     if(num <= 2)
  17.         return 1;
  18.  
  19.     return fibo(num-1) + fibo(num-2);
  20. }
  21.  
  22. /*
  23.  * Main function to call "fibo" in a loop,
  24.  * and display the result.
  25.  */
  26. main()
  27. {
  28.     int i;
  29.  
  30.     for(i=1; i <= MAXFIB; ++i)
  31.         printf("Fibonacci(%u) = %u\n", i, fibo(i));
  32. }
  33.